package com.bdqn.redsi;

import jakarta.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTemplateTester {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Test
    public void stringRedisTemplateTest(){
        stringRedisTemplate.opsForValue().set("test","100",60*10, TimeUnit.SECONDS);
        //val做-1操作
        stringRedisTemplate.boundValueOps("test").increment(-1);
        //根据key获取缓存中的val
        String test = stringRedisTemplate.opsForValue().get("test");
        //val做+1操作
        stringRedisTemplate.boundValueOps("test").increment(1);
        //根据key获取过期时间
        Long expire = stringRedisTemplate.getExpire("test", TimeUnit.SECONDS);
        //根据key删除缓存
        stringRedisTemplate.delete("test");
        //判断key是否存在返回boolean值
        stringRedisTemplate.hasKey("546545");
        //向指定key中存放set集合
        stringRedisTemplate.opsForSet().add("red_123","1","2","3");
        //设置过期时间
        stringRedisTemplate.expire("red_123",1000, TimeUnit.MILLISECONDS);
        //根据key查看集合中是否存在指定数据
        stringRedisTemplate.opsForSet().isMember("red_123","1");
        //根据key获取set集合
        stringRedisTemplate.opsForSet().members("red_123");
    }
    public void redisTemplateTest(){
        //操作字符串
        redisTemplate.opsForCluster();
        //操作hash
        redisTemplate.opsForHash();
        //操作list
        redisTemplate.opsForList();
        //操作set
        redisTemplate.opsForSet();
        //操作zset
        redisTemplate.opsForZSet();
    }
    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("name","czkt");
        Assert.assertEquals("czkt",stringRedisTemplate.opsForValue().get("name"));
    }
}
